<?PHP
$PageID=7;
$nohead=1;
//不需要头部
include_once('./core/common.php');
//全局文件

if (isset($_POST['Verification'])){//验证是否点击(有没有验证码传入)
	if ($_POST['Verification'] == $_SESSION['Verification']){//验证码正确，开始链接数据库判断用户
		if (isset($_POST['Account']) && isset($_POST['Password'])){//如果用户名和密码都不为空
			$sql = "select * from users where Account ='".$_POST['Account']."' AND Password = '".$_POST['Password']."'";
			//检测数据库是否有对应的username和password的sql
			$ret = run_sql($sql);
			if(mysqli_num_rows($ret)){
				//0 false 1 true
				//合格，给予在线状态
				$idsearch=mysqli_fetch_assoc ($ret);
				//结果读取到数组
				$_SESSION['UID']=$idsearch["UID"];
				$_SESSION['Name']=$idsearch["Name"];
				$_SESSION['Avatar']=$idsearch["Avatar"];
				
				//获取授权页面IDs
				$sql = "select * from powergroup where ID ='".$idsearch['UserGroup']."'";
				$ret = run_sql($sql);
				$powersearch=mysqli_fetch_assoc ($ret);
				//结果读取到数组
				$_SESSION['PageIDs']=$powersearch['PageIDs'];
				
				//写入SESSION
				$_SESSION['Session']=md5($idsearch["Name"]  . uniqid() . $idsearch["UID"] .  date('Y-m-d', time()) .  session_id());
				$sql = "UPDATE `users` SET `Session` = '".$_SESSION['Session']."' ,  `lastlogin` = unix_timestamp(NOW()) WHERE `users`.`UID` = ".$idsearch['UID'];
				//更新数据库中的SESSION和最后登录时间
				$mydb = mysqli_connect($Config_DB_url,$Config_DB_username,$Config_DB_password,$Config_DB_dbname,$Config_DB_point);
				//链接数据库进行更新
				if(!$mydb){ die(mysqli_connect_error()); }
				//链接失败输出问题
				mysqli_query($mydb,'SET NAMES UTF8');
				$ret = mysqli_query($mydb,$sql);
				if(mysqli_affected_rows($mydb)==1){
					echo "登录成功";
					//header("refresh:0;url=./index.php");//如果成功跳转至welcome.html页面
					exit;
				} else{ echo "内部错误,登录失败,请重试"; }
				
			} else{ echo "用户名或密码错误"; }
		} else{ echo "表单填写不完整"; }
	} else{ echo "验证码输入错误，请重新输入"; }
	exit();
}
?>

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8" />
    <title>考勤管理平台登录</title>
    <style type="text/css">
      body {
        background-image: url("<?php echo bing_pic(1); ?>");
      }

      .container {
        width: 420px;
        height: 300px;
        min-height: 300px;
        max-height: 300px;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        padding: 20px;
        z-index: 130;
        border-radius: 8px;
        box-shadow: 0 3px 18px rgba(100, 0, 0, 0.5);
        font-size: 20px;
        background-color: rgba(255, 255, 255, 0.4);
      }
      .close {
        background-color: white;
        border: none;
        font-size: 18px;
        margin-left: 410px;
        margin-top: -10px;
      }

      .layui-input {
        border-radius: 5px;
        width: 300px;
        height: 40px;
        font-size: 15px;
      }
      .layui-form-item {
        margin-left: -20px;
      }
      #logoid {
        margin-top: 10px;
        padding-left: 55px;
        padding-bottom: 15px;
      }
      .layui-btn {
        margin-left: -50px;
        border-radius: 5px;
        width: 350px;
        height: 40px;
        font-size: 15px;
      }
      .verity {
        width: 120px;
      }
      .font-set {
        font-size: 13px;
        text-decoration: none;
        margin-left: 120px;
      }
      a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <!-- 账号登录 -->
    <div class="container">
      <form class="layui-form" name="login" action="login.php" method="post">
        <div class="layui-container">
          <div class="layui-row">
            <div class="layui-col-md9">
              <img
                id="logoid"
                src="./assets/images/logo-login.png"
                height="50"
              />
            </div>
          </div>
        </div>
        <div class="layui-form-mid layui-word-aux"></div>

        <div class="layui-form-item">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input
              type="text"
              name="Account"
              required
              lay-verify="required"
              placeholder="请输入用户名"
              autocomplete="off"
              class="layui-input"
            />
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label">密码</label>
          <div class="layui-input-inline">
            <input
              type="password"
              name="Password"
              required
              lay-verify="required"
              placeholder="请输入密码"
              autocomplete="off"
              class="layui-input"
            />
          </div>
          <!-- <div class="layui-form-mid layui-word-aux">辅助文字</div> -->
        </div>

        <div class="layui-form-item form_code">
          <div class="layui-container">
            <div class="layui-row">
              <div class="layui-col-md1">
                <label class="layui-form-label">验证码&nbsp;&nbsp;</label>
              </div>

              <div class="layui-col-md2">
                <div class="layui-input-inline">
                  <input
                    type="text"
                    name="Verification"
                    required
                    lay-verify="required"
                    placeholder="请输入验证码"
                    autocomplete="off"
                    class="layui-input"
                  />
                </div>
              </div>
              <div class="layui-col-md2">
                <img
                  src="./core/Verification.php"
                  onClick="this.src='./core/Verification.php?nocache='+Math.random()"
                  style="cursor: hand"
                  class="layui-input verity"
                  alt="点击换一张"
                />
              </div>
            </div>
          </div>
          <!-- <div class="layui-form-mid layui-word-aux">辅助文字</div> -->
        </div>

        <!-- 
			  <div class="layui-form-item">
				    <label class="layui-form-label">记住我</label>
				    <div class="layui-input-block">
				      <input type="checkbox"  checked ="checked" name="savepass" lay-skin="switch" lay-text="保存|不保存">
				    </div>
			  </div> -->

        <div class="layui-form-item">
          <div class="layui-input-block">
            <button
              type="submit"
              value="登录"
              name="submit"
              class="layui-btn layui-btn-lg layui-btn-radius"
              lay-submit
              lay-filter="formDemo"
            >
              登录
            </button>
            <!-- 新版 -->
          </div>
        </div>
      </form>
      <!-- 账号登录 -->

      <!--<a href="signup.php" class="font-set">点我授权</a> -->
    </div>

    <script>
      layui.use(["form", "layedit", "laydate"], function () {
        var form = layui.form,
          layer = layui.layer,
          layedit = layui.layedit,
          laydate = layui.laydate;

        //日期
        /* laydate.render({
			    elem: '#date'
			  });
			  laydate.render({
			    elem: '#date1'
			  });
			  
			  //创建一个编辑器
			  var editIndex = layedit.build('LAY_demo_editor');
			 
			  //自定义验证规则
			  form.verify({
			    title: function(value){
			      if(value.length < 5){
			        return '标题至少得5个字符啊';
			      }
			    }
			    ,pass: [
			      /^[\S]{6,12}$/
			      ,'密码必须6到12位，且不能出现空格'
			    ]
			    ,content: function(value){
			      layedit.sync(editIndex);
			    }
			  });
			  
			  //监听指定开关
			  form.on('switch(switchTest)', function(data){
			    layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
			      offset: '6px'
			    });
			    layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
			  });*/

        //监听提交
        form.on("submit(demo1)", function (data) {
          layer.alert(JSON.stringify(data.field), {
            title: "最终的提交信息"
          });
          return false;
        });

        //表单初始赋值
        /* form.val('example', {
			    "username": "贤心" // "name": "value"
			    ,"password": "123456"
			    ,"interest": 1
			    ,"like[write]": true //复选框选中状态
			    ,"close": true //开关状态
			    ,"sex": "女"
			    ,"desc": "我爱 layui"
			  })*/
      });
    </script>
  </body>
</html>
